package com.igoal.web.security;

import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.time.DateUtils;

public final class LoginStatus {

	private static int DURATION = 60;
	private static Map<String, Date> onlineUsers;

	static {
		onlineUsers = Collections.synchronizedMap(new HashMap<String, Date>());
	}

	public synchronized static void login(String username) {
		refreshStatus(username);
	}

	public synchronized static void logout(String username) {
		onlineUsers.remove(username);
	}

	public static void refreshStatus(String username) {
		onlineUsers.put(username, DateUtils.addMinutes(new Date(), DURATION));
	}

	public static boolean isOnline(String username) {
		Date onlineTill = onlineUsers.get(username);
		if (onlineTill != null) {
			if (onlineTill.after(new Date())) {
				return true;
			} else {
				onlineUsers.remove(username);
			}
		}
		return false;
	}
}
